package com.jlastu.znb.view;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.jlastu.znb.R;
import com.jlastu.znb.contract.CollectContract;
import com.jlastu.znb.entity.Goods;
import com.jlastu.znb.adapter.GoodsCollectListAdapter;

import java.util.ArrayList;
import java.util.List;
import com.jlastu.znb.presenter.CollectPresenter;

public class CollectActivity extends AppCompatActivity implements CollectContract.ICollectView{
    private ImageView mBack;
    private TextView mTextView1;
    private TextView mTextView2;
    private TextView mTextView3;
    private TextView mManage;
    private GoodsCollectListAdapter mGoodsCollectListAdapter;
    private CollectContract.ICollectPresenter mCollectPresenter;
    private RecyclerView mGoodsRecyclerView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect);

        mCollectPresenter = new CollectPresenter(this, this);

        // 使用成员变量mGoodsRecyclerView而不是局部变量recyclerView
        mGoodsRecyclerView = findViewById(R.id.collect_listView);
        StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL); // 假设您想要2列
        mGoodsRecyclerView.setLayoutManager(staggeredGridLayoutManager); // 使用成员变量
        mGoodsCollectListAdapter = new GoodsCollectListAdapter(new ArrayList<>());
        mGoodsRecyclerView.setAdapter(mGoodsCollectListAdapter);
        mManage = findViewById(R.id.manage);

        // 添加以下代码来设置布局管理器
        mTextView1 = findViewById(R.id.collect_title1);
        mTextView2 = findViewById(R.id.collect_title2);
        mTextView3 = findViewById(R.id.collect_title3);

        // 初始化时默认选中第一个标签
        setTabSelected(mTextView1);

        // 设置点击事件
        mTextView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTabSelected(mTextView1);
                mCollectPresenter.getGoodsList(); // 获取已收藏商品列表
            }
        });
        mTextView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTabSelected(mTextView2);
            }
        });
        mTextView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTabSelected(mTextView3);
            }
        });
        mBack = findViewById(R.id.back);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        mCollectPresenter.getGoodsList();
    }
    private void setTabSelected(TextView selectedTab) {
        // 重置所有标签为未选中状态
        mTextView1.setSelected(false);
        mTextView2.setSelected(false);
        mTextView3.setSelected(false);

        // 将被点击的标签设置为选中状态
        selectedTab.setSelected(true);
    }


    @Override
    public void showGoodsList(List<Goods> goodsList) {
        // 过滤出已收藏的商品
        List<Goods> collectedGoods = new ArrayList<>();
        for (Goods goods : goodsList) {
            if (goods.isCollect()) { // 假设Goods类有一个isCollect方法来检查商品是否被收藏
                collectedGoods.add(goods);
            }
        }
        // 更新适配器的数据
        mGoodsCollectListAdapter.setGoodsList(collectedGoods);
        // 通知适配器数据已更改
        mGoodsCollectListAdapter.notifyDataSetChanged();
    }
}
